<?php namespace WoXuanWang\User\Commands;

use WoXuanWang\User\Models\Permission;
use WoXuanWang\User\Models\Role;
use WoXuanWang\User\Repositories\RoleRepository;

use Illuminate\Console\Command;
use ReflectionClass;

class RestorePermissions extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'woxuanwang:restorePermissions';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'restore roles permissions';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $controllers = glob('WoXuanWang/*/Controllers/*');
        $permissions = [];
        foreach ($controllers as $controller) {
            $controllerClass = str_replace('/', '\\', rtrim($controller, '.php'));
            $controllerName = basename($controller, 'Controller.php');
            $reflection = new ReflectionClass($controllerClass);
            $methods = $reflection->getMethods();
            foreach ($methods as $method) {
                if ($method->name == '__construct' || !$method->isPublic() || $method->class != $controllerClass) {
                    continue;
                }
                $permissionName = $controllerName . ',' . $method->name;
                $permission = Permission::firstOrCreate(['name' => $permissionName]);
                $permissions[] = $permission->id;
            }
        }
        // Attach to Role admin
        $adminRole = Role::firstOrCreate(['name' => RoleRepository::ROLE_ADMIN]);
        $adminRole->permissions()->sync($permissions);

        // Attach to Role customer
        $customerPermissions = [
            Permission::whereName('Company,index')->first()->id,
            Permission::whereName('Company,show')->first()->id,
            Permission::whereName('Company,update')->first()->id,

            Permission::whereName('Customer,index')->first()->id,
            Permission::whereName('Customer,show')->first()->id,
            Permission::whereName('Customer,store')->first()->id,
            Permission::whereName('Customer,update')->first()->id,
            Permission::whereName('Customer,destroy')->first()->id,

            Permission::whereName('Project,index')->first()->id,
            Permission::whereName('Project,show')->first()->id,
            Permission::whereName('Project,update')->first()->id,

            Permission::whereName('Organization,index')->first()->id,
            Permission::whereName('Organization,show')->first()->id,

            Permission::whereName('Supervisor,index')->first()->id,
            Permission::whereName('Supervisor,show')->first()->id,

            Permission::whereName('Solution,index')->first()->id,
            Permission::whereName('Solution,show')->first()->id,

            Permission::whereName('Point,index')->first()->id,
            Permission::whereName('Point,show')->first()->id,

            Permission::whereName('MonitorMethod,index')->first()->id,
            Permission::whereName('MonitorMethod,show')->first()->id,

            Permission::whereName('DeviceType,index')->first()->id,
            Permission::whereName('DeviceType,show')->first()->id,

            Permission::whereName('Device,index')->first()->id,
            Permission::whereName('Device,show')->first()->id,

            Permission::whereName('Weather,realtime')->first()->id,
            Permission::whereName('Weather,rain')->first()->id,

            Permission::whereName('PatrolTarget,index')->first()->id,
            Permission::whereName('PatrolTarget,show')->first()->id,
            Permission::whereName('PatrolTarget,store')->first()->id,
            Permission::whereName('PatrolTarget,update')->first()->id,
            Permission::whereName('PatrolTarget,destroy')->first()->id,
        ];
        $customerRole = Role::whereName(['name' => RoleRepository::ROLE_CUSTOMER])->first();
        $customerRole->permissions()->sync($customerPermissions);

        // Attach to Role supervisor
        $supervisorPermissions = [
            Permission::whereName('Organization,index')->first()->id,
            Permission::whereName('Organization,show')->first()->id,
            Permission::whereName('Organization,update')->first()->id,

            Permission::whereName('Supervisor,index')->first()->id,
            Permission::whereName('Supervisor,show')->first()->id,
            Permission::whereName('Supervisor,store')->first()->id,
            Permission::whereName('Supervisor,update')->first()->id,
            Permission::whereName('Supervisor,destroy')->first()->id,

            Permission::whereName('Project,index')->first()->id,
            Permission::whereName('Project,show')->first()->id,
            Permission::whereName('Project,store')->first()->id,
            Permission::whereName('Project,update')->first()->id,

            Permission::whereName('Solution,index')->first()->id,
            Permission::whereName('Solution,show')->first()->id,
            Permission::whereName('Solution,store')->first()->id,
            Permission::whereName('Solution,update')->first()->id,
            Permission::whereName('Solution,destroy')->first()->id,

            Permission::whereName('Point,index')->first()->id,
            Permission::whereName('Point,show')->first()->id,
            Permission::whereName('Point,store')->first()->id,
            Permission::whereName('Point,update')->first()->id,
            Permission::whereName('Point,destroy')->first()->id,

            Permission::whereName('MonitorMethod,index')->first()->id,
            Permission::whereName('MonitorMethod,show')->first()->id,
            Permission::whereName('MonitorMethod,store')->first()->id,
            Permission::whereName('MonitorMethod,update')->first()->id,
            Permission::whereName('MonitorMethod,destroy')->first()->id,

            Permission::whereName('DeviceType,index')->first()->id,
            Permission::whereName('DeviceType,show')->first()->id,
            Permission::whereName('DeviceType,store')->first()->id,
            Permission::whereName('DeviceType,update')->first()->id,
            Permission::whereName('DeviceType,destroy')->first()->id,

            Permission::whereName('Device,index')->first()->id,
            Permission::whereName('Device,show')->first()->id,
            Permission::whereName('Device,store')->first()->id,
            Permission::whereName('Device,update')->first()->id,
            Permission::whereName('Device,destroy')->first()->id,

            Permission::whereName('Weather,realtime')->first()->id,
            Permission::whereName('Weather,rain')->first()->id,

            Permission::whereName('PatrolTarget,index')->first()->id,
            Permission::whereName('PatrolTarget,show')->first()->id,
            Permission::whereName('PatrolTarget,store')->first()->id,
            Permission::whereName('PatrolTarget,update')->first()->id,
            Permission::whereName('PatrolTarget,destroy')->first()->id,
        ];
        $supervisorRole = Role::whereName(['name' => RoleRepository::ROLE_SUPERVISOR])->first();
        $supervisorRole->permissions()->sync($supervisorPermissions);
    }
}
